package com.mkvld.appietijdenv2.fragments;

import com.mkvld.appietijdenv2.activities.ActMainV9;
import com.mkvld.appietijdenv2.data.DayShifts;
import com.mkvld.appietijdenv2.data.Utils;
import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.dialogs.DialNewShiftV9;
import com.mkvld.appietijdenv2.views.ViewShift;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class FragShiftsV9 extends PeriodFragV9 {
	private static int[][] dayIds = {
		{
			R.id.fs_week0_day0, R.id.fs_week0_day1, R.id.fs_week0_day2, R.id.fs_week0_day3,
			R.id.fs_week0_day4, R.id.fs_week0_day5, R.id.fs_week0_day6
		},
		{
			R.id.fs_week1_day0, R.id.fs_week1_day1, R.id.fs_week1_day2, R.id.fs_week1_day3,
			R.id.fs_week1_day4, R.id.fs_week1_day5, R.id.fs_week1_day6
		},
		{
			R.id.fs_week2_day0, R.id.fs_week2_day1, R.id.fs_week2_day2, R.id.fs_week2_day3,
			R.id.fs_week2_day4, R.id.fs_week2_day5, R.id.fs_week2_day6
		},
		{
			R.id.fs_week3_day0, R.id.fs_week3_day1, R.id.fs_week3_day2, R.id.fs_week3_day3,
			R.id.fs_week3_day4, R.id.fs_week3_day5, R.id.fs_week3_day6
		}
	};
	
	private static int[] weekTitleIds = { R.id.fs_week0_title, R.id.fs_week1_title, R.id.fs_week2_title, R.id.fs_week3_title }; 

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.frag_shifts, null, false);
		init(v);
		update(v);
		return v;
	}
	@Override
	public void update() {
		if (getView() != null)
			update(getView());
	}
	
	public void setClickHandlers(ViewShift vs) {
		vs.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				ViewShift viewShift = (ViewShift)view;
				if (viewShift.getStart() == -1)
					showNewShiftDialog(getYear(), getPeriod() * 4 + viewShift.getWeek(), viewShift.getDay());
				else
					shiftSelected(viewShift);
			}
		});
	}
	
	public abstract void shiftSelected(ViewShift vs);

	protected void showNewShiftDialog(int year, int week, int day) {
		DialNewShiftV9.showNew(new DialNewShiftV9() ,getActivity(), year, week, day);
		
	}

	private void init(View root) {
		for (int week = 0; week < dayIds.length; week++) {
			for (int day = 0; day < dayIds[week].length; day++) {
				LinearLayout dayView = (LinearLayout)root.findViewById(dayIds[week][day]);
				((ViewShift)dayView.getChildAt(1)).setDay(week, day);
				setClickHandlers((ViewShift) dayView.getChildAt(1));
			}
		}
	}
	
	private void update(View root) {
		
		((TextView)root.findViewById(R.id.fs_title)).setText(getResources().getString(R.string.fs_period_title, getPeriod() + 1, getYear()));
		for (int week = 0; week < dayIds.length; week++) {
			DayShifts[] weekShifts = ActMainV9.periodShifts.getWeekShifts(week).getShifts();
			String weekTitle = getResources().getString(R.string.fs_week_title, getPeriod() * 4 + week + 1);
			if (getPeriod() * 4 + week == Utils.getCurrentWeek())
				weekTitle += getResources().getString(R.string.fs_current_week);
			((TextView)root.findViewById(weekTitleIds[week])).setText(weekTitle);
			for (int day = 0; day < dayIds[week].length; day++) {
				LinearLayout dayView = (LinearLayout)root.findViewById(dayIds[week][day]);
				DayShifts dayShifts = weekShifts[day];
				
				// make sure there are enough ViewShift views
				int addViews = dayShifts.size() - (dayView.getChildCount() - 1);
				if (dayShifts.size() == 0)
					addViews = 2 - dayView.getChildCount();
				if (addViews > 0) {
					while (addViews-- > 0) {
						ViewShift vs = new ViewShift(getActivity());
						vs.setDay(week, day);
						setClickHandlers(vs);
						dayView.addView(vs);
					}
					addViews++;
				}
				if (addViews < 0) {
					while (addViews++ < 0) {
						dayView.removeViewAt(dayView.getChildCount() - 1);
					}
				}
				if (dayShifts.size() == 0)
					((ViewShift)dayView.getChildAt(1)).setTimes(-1, -1);
				for (int i = 0; i < dayShifts.size(); i++) {
					ViewShift vs = (ViewShift)dayView.getChildAt(i + 1);
					vs.setTimes(dayShifts.getShifts().get(i).getStart(), dayShifts.getShifts().get(i).getEnd());
				}
			}
		}
	}

}
